package com.mes.common.condition;

import java.io.Serializable;


/**
 * <p>
 * 用于表示一个ConditionObject和它的连接类型，如果没有给出连接类型就默认为and
 * </p>
 * 
 * <p>
 * 2009-07
 * </p>
 * 
 * @author lhy
 * 
 */
public class ConditionObjectLink implements Serializable {
	public static final String AND_TYPE = "and";
	public static final String OR_TYPE = "or";
	public static final String END_TYPE = "end";

	private ConditionObject conditionObject;
	private String linkType;

	public ConditionObjectLink(String key, String operator, String value,
			String keyType) {
		super();
		conditionObject = new ConditionObject(key, operator, value, keyType);
		this.linkType = ConditionObjectLink.AND_TYPE;
	}

	public ConditionObjectLink(String key, String operator, String value,
			String keyType, String linkType) {
		super();
		conditionObject = new ConditionObject(key, operator, value, keyType);
		this.linkType = linkType;
	}

	public ConditionObjectLink() {
		super();
		this.conditionObject = new ConditionObject();
		this.linkType = ConditionObjectLink.AND_TYPE;
	}

	public ConditionObjectLink(ConditionObject conditionObject, String linkType) {
		super();
		this.conditionObject = conditionObject;
		this.linkType = linkType;
	}

	public ConditionObjectLink(ConditionObject conditionObject) {
		super();
		this.conditionObject = conditionObject;
		this.linkType = ConditionObjectLink.AND_TYPE;
	}

	public ConditionObject getConditionObject() {
		return conditionObject;
	}

	public void setConditionObject(ConditionObject conditionObject) {
		this.conditionObject = conditionObject;
	}

	public String getLinkType() {
		return linkType;
	}

	public void setLinkType(String linkType) {
		this.linkType = linkType;
	}

	public String toString() {
		if (isLastCondition(this)) {
			return this.getConditionObject().toString();
		} else if ("or".equalsIgnoreCase(this.getLinkType())) {
			return this.getConditionObject().toString() + " or ";
		}
		// 其余都按照and处理
		return this.getConditionObject().toString() + " and ";
	}

	/**
	 * 返回各个属性的连接，其中value用问号代替
	 * 
	 * @return
	 */
	public String toStringWithoutValue() {
		if (isLastCondition(this)) {
			return this.getConditionObject().toStringWithoutValue();
		} else if ("or".equalsIgnoreCase(this.getLinkType())) {
			return this.getConditionObject().toStringWithoutValue() + " or ";
		}
		// 其余都按照and处理
		return this.getConditionObject().toStringWithoutValue() + " and ";
	}

	/**
	 * 返回各个属性的连接
	 * 
	 * @return
	 */
	public String toStringWithValue() {
		if (isLastCondition(this)) {
			return this.getConditionObject().toString();
		} else if ("or".equalsIgnoreCase(this.getLinkType())) {
			return this.getConditionObject().toString() + " or ";
		}
		// 其余都按照and处理
		return this.getConditionObject().toString() + " and ";
	}

	public boolean isLastCondition(ConditionObjectLink conditionObjectLink) {
		return "end".equalsIgnoreCase(conditionObjectLink.getLinkType());
	}
}
